package com.aeye.mbr.upms.server.controller.manage;

import com.aeye.mbr.common.listener.ApplicationContextListener;
import com.aeye.mbr.upms.common.constant.UpmsCacheConstants;
import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.aeye.mbr.common.base.BaseController;
import com.aeye.mbr.common.result.BootstrapTableParams;
import com.aeye.mbr.common.ui.validator.LengthValidator;
import com.aeye.mbr.upms.common.constant.UpmsResult;
import com.aeye.mbr.upms.common.constant.UpmsResultConstant;
import com.aeye.mbr.upms.dao.model.UpmsOrganization;
import com.aeye.mbr.upms.dao.model.UpmsUser;
import com.aeye.mbr.upms.rpc.api.UpmsOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 组织controller
 * Created by shuaeye.mbr on 2017/2/6.
 */
@Controller
@Api(value = "组织管理", description = "组织管理")
@RequestMapping("/manage/organization")
public class UpmsOrganizationController extends BaseController {

    private static Logger _log = LoggerFactory.getLogger(UpmsOrganizationController.class);

    @Autowired
    private UpmsOrganizationService upmsOrganizationService;

    @ApiOperation(value = "组织首页")
    @RequiresPermissions("upms:organization:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/organization/index.jsp";
    }

    @ApiOperation(value = "组织列表")
    @RequiresPermissions("upms:organization:read")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(BootstrapTableParams params,String parentCode) {
        return upmsOrganizationService.selectUpmsOrganizationByParams(params,parentCode);
    }

    @ApiOperation(value = "新增组织")
    @RequiresPermissions("upms:organization:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(HttpServletRequest request, Model model) {
        UpmsOrganization entity = new UpmsOrganization();
        List<UpmsOrganization> organizations = upmsOrganizationService.select(entity);
        model.addAttribute("organizations", organizations);
        return "/manage/organization/create.jsp";
    }

    @ApiOperation(value = "新增组织")
    @RequiresPermissions("upms:organization:create")
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Object create(UpmsOrganization upmsOrganization) {
        ComplexResult result = FluentValidator.checkAll()
                .on(upmsOrganization.getName(), new LengthValidator(1, 20, "机构名称"))
                .on(upmsOrganization.getContactName(),new LengthValidator(0, 20, "联系人"))
                .on(upmsOrganization.getContactPhone(),new LengthValidator(0, 20, "联系电话"))
                .on(upmsOrganization.getLongitude(),new LengthValidator(0, 20, "经度"))
                .on(upmsOrganization.getContactName(),new LengthValidator(0, 20, "纬度"))
                .on(upmsOrganization.getType(),new LengthValidator(1, 2, "类型"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        int count = upmsOrganizationService.addUpmsOrganization(upmsOrganization);
        if(count > 0)
        {
            ApplicationContextListener.refreshCache(UpmsCacheConstants.UPMS_ORG_CACHE,String.valueOf(System.currentTimeMillis()));
        }
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除组织")
    @RequiresPermissions("upms:organization:delete")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        int count = upmsOrganizationService.deleteUpmsOrganization(ids);
        if(count > 0)
        {
            ApplicationContextListener.refreshCache(UpmsCacheConstants.UPMS_ORG_CACHE,String.valueOf(System.currentTimeMillis()));
        }
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改组织")
    @RequiresPermissions("upms:organization:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(HttpServletRequest request, @PathVariable("id") int id, ModelMap modelMap) {
        UpmsOrganization organization = upmsOrganizationService.selectByPrimaryKey(id);
        modelMap.put("organization", organization);
        UpmsOrganization entity = new UpmsOrganization();
        List<UpmsOrganization> organizations = upmsOrganizationService.select(entity);
        modelMap.put("organizations", organizations);


        return "/manage/organization/update.jsp";
    }

    @ApiOperation(value = "修改组织")
    @RequiresPermissions("upms:organization:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Integer id, UpmsOrganization upmsOrganization) {
        ComplexResult result = FluentValidator.checkAll()
                .on(upmsOrganization.getName(), new LengthValidator(1, 20, "名称"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
        }
        upmsOrganization.setOrganizationId(id);
        int count = upmsOrganizationService.updateByPrimaryKeySelective(upmsOrganization);
        return new UpmsResult(UpmsResultConstant.SUCCESS, count);
    }

}
